//
//  TSZWelcomeViewController.swift
//  微博天下
//

import UIKit
import SDWebImage
class TSZWelcomeViewController: UIViewController {
    /// 图像底部约束
    private var iconBottomCons: NSLayoutConstraint?
    override func viewDidLoad() {
        super.viewDidLoad()

        //准备UI界面
        perpareUI()
        
        //加载用户头像
        if let urlString = TSZUserAcountModel.sharedUserAccount()?.avatar_large{
            iconView.sd_setImageWithURL(NSURL(string: urlString)!)
        }
    }
    
    //在试图加载完成后 显示动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
         // 提示：修改约束不会立即生效，添加了一个标记，统一由自动布局系统更新约束
        
        iconBottomCons?.constant =  -UIScreen.mainScreen().bounds.height - iconBottomCons!.constant
        //动画
        UIView.animateWithDuration(1.5, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //强制更新约束
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                //发送通知 切换控制器
                NSNotificationCenter.defaultCenter().postNotificationName(TSZRootViewControllerSwitchNotification, object: true)
        }
    }
    //MARK 准备UI
    private func perpareUI(){
        
        //添加背景
        view.addSubview(backGroundView)
        view.addSubview(iconView)
        view.addSubview(labelView)

        //背景图片
        backGroundView.ff_Fill(view)
                let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset: CGPoint(x: 0, y: -160))
        // 记录底边约束
        iconBottomCons = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
        //文字标题
        labelView.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
    }

    //MARK: -懒加载控件
    
    //背景颜色
    private lazy var backGroundView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //用户头像
    private lazy var iconView:UIImageView = {
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius  = 30
        
        return iv
    }()
    
    //用户文字
    private lazy var labelView: UILabel = {
            let label = UILabel()
            label.textColor = UIColor.purpleColor()
            let text = TSZUserAcountModel.sharedUserAccount()!.name!
            label.text = "欢迎您：\(text)"
            label.sizeToFit()
            return label
        }()
}
